package com.sc.sc250601.cook.controller;

import com.sc.sc250601.cook.pojo.Result;
import com.sc.sc250601.cook.pojo.User;
import com.sc.sc250601.cook.service.UserService;
import com.sc.sc250601.cook.util.UpDowns;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/getUsers")
    public Result getUsers(String username) {
        List<User> users = userService.getUsers(username);
        return new Result(1, "success", users);
    }
    @RequestMapping("/login")
    public Result Login(@RequestBody User user) {
        System.out.println(user.getUsername()+" :"+user.getPassword());
        User u=userService.Login(user);
        if(u!=null){
            return new Result(1, "登录成功", u);
        }
        return new Result(0, "登录失败");
    }
    @RequestMapping("/reg")
    public Result Reg(User user, MultipartFile myHead) {
        String fileName= UpDowns.upload(myHead);
        user.setAvatar("/api/upload/"+fileName);
        user.setIsVip(false);
        int n=userService.Reg(user);
        if(n>0){
            return new Result(1, "注册成功");
        }
        return new Result(0, "注册失败");
    }
}
